package com.yunxi.service.dubboService.impl.sto;

import com.alibaba.cloud.context.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.model.order.PmsVehicleStock;
import com.yunxi.model.sto.StoStorageCheck;
import com.yunxi.model.sto.VehicleOffline;
import com.yunxi.service.localService.sto.StoStorageCheckService;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import com.yunxi.service.localService.sto.VehicleStockMService;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.StoStorageCheckDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存盘点 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-08-16
 */
public class StoStorageCheckDubboServiceImpl implements StoStorageCheckDubboService {

    @Autowired
    @Qualifier(value = "stoStorageCheckServiceImpl")
    StoStorageCheckService stoStorageCheckService;

    @Autowired
    @Qualifier(value = "vehicleStockMServiceImpl")
    VehicleStockMService vehicleStockMService;

    @Autowired
    @Qualifier(value = "vehicleOfflineServiceImpl")
    VehicleOfflineService vehicleOfflineService;


    @Override
    public StoStorageCheckVO getById(String id) throws Exception {
        StoStorageCheck stoStorageCheck = stoStorageCheckService.getById(id);
        if(stoStorageCheck == null)throw new Exception("操作失败,无法根据主键:"+id+"找到记录!");
        StoStorageCheckVO stoStorageCheckVO = new StoStorageCheckVO();
        BeanUtils.copyProperties(stoStorageCheck, stoStorageCheckVO);
        return stoStorageCheckVO;
    }

    @Override
    public void saveOrUpdate(StoStorageCheckVO stoStorageCheckVO) throws Exception {
        StoStorageCheck stoStorageCheck = new StoStorageCheck();
        BeanUtils.copyProperties(stoStorageCheckVO, stoStorageCheck);
        stoStorageCheckService.saveOrUpdate(stoStorageCheck);
    }

    @Override
    public void removeById(String id) {
        stoStorageCheckService.removeById(id);
    }

    @Override
    public PageInfo<StoStorageCheckExportVO> findPageByVO(StoStorageCheckVO stoStorageCheckVO, Integer pageNum, Integer pageSize) throws Exception {
        PageInfo<StoStorageCheckExportVO> pageInfo = stoStorageCheckService.pageByVo(stoStorageCheckVO, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 导出
     * @param stoStorageCheckVO
     * @return
     * @throws Exception
     */
    @Override
    public List<StoStorageCheckExportVO> export(StoStorageCheckVO stoStorageCheckVO) throws Exception {
        return stoStorageCheckService.export(stoStorageCheckVO);
    }

    @Override
    public void removeByListVo(List<StoStorageCheckVO> stoStorageCheckVOList) throws Exception {
        stoStorageCheckService.removeByListVo(stoStorageCheckVOList);
    }

    @Override
    public List listByVo(StoStorageCheckVO stoStorageCheckVO) {
        return stoStorageCheckService.listByVo(stoStorageCheckVO);
    }

    /**
     * 根据盘点计划名称删除计划
     * @param checkName
     * @throws Exception
     */
    @Override
    public void del(String checkName) throws Exception {
        QueryWrapper<StoStorageCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CHECK_NAME",checkName);
        List<StoStorageCheck> list = stoStorageCheckService.list(queryWrapper);
        if(list == null || list.size() <= 0)throw new Exception("操作失败,盘点计划:"+checkName+"不存在!");
        for (StoStorageCheck stoStorageCheck : list){
            stoStorageCheck.setCheckStatus(3);
            stoStorageCheckService.saveOrUpdate(stoStorageCheck);
        }
    }

    /**
     * 新增盘点计划
     * @param stoStorageCheckAddVO
     * @throws Exception
     */
    @Override
    public void newPlan(StoStorageCheckAddVO stoStorageCheckAddVO) throws Exception {
        String checkName = stoStorageCheckAddVO.getCheckName();//盘点计划名称
        String checkScope = stoStorageCheckAddVO.getCheckScope();
//        String partnerCode = stoStorageCheckAddVO.getPartnerCode();
        List<String> vinList1 = stoStorageCheckAddVO.getVinList();

        if(StringUtils.isEmpty(checkScope) && (vinList1== null || vinList1.size() <= 0))throw new Exception("操作失败,请选择盘点范围!");
        //去空格
        if(vinList1 != null && vinList1.size() > 0){
            for(int i = 0;i<vinList1.size();i++){
                String s = vinList1.get(i).replaceAll("\\s+", "");
                vinList1.set(i,s);
            }
        }
        //校验计划名称不可重复,不可为空
        checkNameIsReapet(checkName, "partnerCode");
        List<String> vinList = new ArrayList<>();
        //所有在库车辆
        if(StringUtils.isNotEmpty(checkScope) && checkScope.equals("-1")){
            QueryWrapper<PmsVehicleStock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PARTNER_CODE","");
            queryWrapper.eq("STORAGE_STATUS",1);
            List<PmsVehicleStock> list = vehicleStockMService.list(queryWrapper);
            if(list != null && list.size() > 0){
                for (PmsVehicleStock pmsVehicleStock : list){
                    vinList.add(pmsVehicleStock.getVin());
                }
            }
            else{
                throw new Exception("操作失败,当前库位库存车数量为0!");
            }
        }
        else if(StringUtils.isNotEmpty(checkScope) && !checkScope.equals("-1")){
            //查询该库区下,存在哪些在库车辆
            vinList = stoStorageCheckService.queryInStockVin(stoStorageCheckAddVO);
            if(vinList == null || vinList.size() <= 0)throw new Exception("操作失败,库区:"+checkScope+"中在库车数量为0!");
        }
        else {
            vinList = vinList1;
        }
        for (String vin :vinList){
            //校验VIN码是否存在
            QueryWrapper<VehicleOffline> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("VIN",vin);
            VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper);
//            if(vehicleOffline == null)throw new Exception("操作失败,VIN码:"+vin+"不存在!");
            StoStorageCheck stoStorageCheck = new StoStorageCheck();
            stoStorageCheck.setVin(vin);
//            stoStorageCheck.setPartnerCode(partnerCode);
            stoStorageCheck.setPlanCheckDate(stoStorageCheckAddVO.getPlanCheckDate());
            stoStorageCheck.setCheckName(stoStorageCheckAddVO.getCheckName());
            if(StringUtils.isNotEmpty(checkScope)){
                stoStorageCheck.setCheckArea(checkScope);//盘点区域
            }
            stoStorageCheckService.saveOrUpdate(stoStorageCheck);
        }
    }


    /**
     * 盘点计划列表
     * @param stoStorageCheckListVO
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<StoStorageCheckListVO> checkPlanList(StoStorageCheckListVO stoStorageCheckListVO, Integer pageNum,Integer pageSize) throws Exception {
        return stoStorageCheckService.checkPlanList(pageNum,pageSize,stoStorageCheckListVO);
    }

    @Override
    public StoStorageCheckDetailVO detail(String vin,String checkName) throws Exception {
        StoStorageCheckVO stoStorageCheckVO = new StoStorageCheckVO();
        stoStorageCheckVO.setVin(vin);
        stoStorageCheckVO.setCheckName(checkName);
        StoStorageCheckDetailVO vo = stoStorageCheckService.detail(stoStorageCheckVO);
        if(vo == null) throw new Exception("操作失败,VIN码:"+vin+"不存在未完成的盘点计划!");
        return vo;
    }

    /**
     * 完成盘点
     * @param stoStorageCheckDetailVO
     * @throws Exception
     */
    @Override
    public void complete(StoStorageCheckDetailVO stoStorageCheckDetailVO) throws Exception {
        String storageCheckId = stoStorageCheckDetailVO.getStorageCheckId();
        StoStorageCheck stoStorageCheck = stoStorageCheckService.getById(storageCheckId);
        if(stoStorageCheck == null)throw new Exception("操作失败,无法根据主键:"+storageCheckId+"找到对应记录!");
        Integer checkStatus = stoStorageCheck.getCheckStatus();
        if(checkStatus != null && !checkStatus.equals(1))throw new Exception("操作失败,该盘点单已完成!");
        stoStorageCheck.setCheckStatus(2);
        stoStorageCheck.setVehicleStatus(stoStorageCheckDetailVO.getVehicleStatus());
        stoStorageCheck.setIsInStorage(stoStorageCheckDetailVO.getIsInStorage());
        stoStorageCheck.setCompleteCheckDate(new Date());
        stoStorageCheck.setRemark(stoStorageCheckDetailVO.getRemark());
        stoStorageCheck.setChecker(stoStorageCheckDetailVO.getChecker());
        stoStorageCheck.setStorageLocationStatus(stoStorageCheckDetailVO.getStorageLocationStatus());
        stoStorageCheck.setIsKeyIn(stoStorageCheckDetailVO.getIsKeyIn());
        stoStorageCheckService.saveOrUpdate(stoStorageCheck);
    }

    /**
     * 根据盘点计划名称查询盘点报告
     * @param checkName
     * @return
     * @throws Exception
     */
    @Override
    public StoStorageCheckReportVO checkReport(String checkName) throws Exception {
        StoStorageCheckReportVO vo = stoStorageCheckService.checkReport(checkName);
        if(vo == null)throw new Exception("操作失败,盘点计划:"+checkName+"不存在!");
        //根据盘点区域,计算系统车辆库存数量,实际车辆数量
        String checkArea = vo.getCheckArea();//盘点区域
        //如果checkArea是空或者'-1',代表全部在库车
        StoStorageCheckReportVO vo1 = new StoStorageCheckReportVO();
        if(StringUtils.isNotEmpty(checkArea) && !checkArea.equals("-1")){
            vo1.setCheckArea(checkArea);
        }
        StoStorageCheckCountVO stoStorageCheckCountVO = stoStorageCheckService.queryVehicleCount(vo1);
        vo.setAllCount(stoStorageCheckCountVO.getAllCount());//系统库存车总数
        vo.setInCount(stoStorageCheckCountVO.getInCount());//系统在库车
        vo.setOutCount(stoStorageCheckCountVO.getOutCount());//系统不在库车
        //不在库车辆(系统在库车-盘点在库车)
        Integer inCount = vo.getInCount();//系统在库车
        Integer actInCount = vo.getActInCount();//盘点在库车
        if(inCount != null && actInCount != null){
            vo.setNotInCount(inCount-actInCount);
        }
        return vo;
    }

    /**
     * 校验盘点名称不可重复
     * @param checkName
     * @param partnerCode
     * @throws Exception
     */
    private void checkNameIsReapet(String checkName, String partnerCode) throws Exception {
        if(StringUtils.isEmpty(checkName))throw new Exception("操作失败,盘点计划名称不可为空!");
        QueryWrapper<StoStorageCheck> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("CHECK_NAME",checkName);
        queryWrapper1.ne("CHECK_STATUS",3);
//        queryWrapper1.eq("PARTNER_CODE",partnerCode);
        List<StoStorageCheck> list1 = stoStorageCheckService.list(queryWrapper1);
        if(list1 != null && list1.size() > 0)throw new Exception("操作失败,盘点计划名称:"+checkName+"已存在,不可重复!");
    }
}
